
//see https://github.com/facundoolano/socketio-auth
/**
 * 
 * 
// send to current request socket client
socket.emit('message', "this is a test");

// sending to all clients except sender
socket.broadcast.emit('message', "this is a test");

// sending to all clients in 'game' room(channel) except sender
socket.broadcast.to('game').emit('message', 'nice game');

// sending to all clients, include sender
io.sockets.emit('message', "this is a test");

// sending to all clients in 'game' room(channel), include sender
io.sockets.in('game').emit('message', 'cool game');

// sending to individual socketid
io.sockets.socket(socketid).emit('message', 'for your eyes only');

socket.leave(data.room);

*/
import SocketIO from 'socket.io';
import fs from 'fs-extra';
module.exports = app => {

    let io = new SocketIO(app.server);
    io.on('connection', async (socket) => {

        console.log("on connection, socket id  is ", socket.id)
        socket.on('disconnect', () => {
          
        });
        socket.on('leave', (msg) => {
           
        });
        socket.on('m:msg', async(msg) => {
            console.log("message is coming, the content is "+msg)
        });
        
        socket.on('m:delete_voice', async(filepath) => {
            console.log("delete file commond is comming "+filepath)
            if(fs.existsSync(filepath)){
                setTimeout(() => {
                    fs.removeSync(filepath);
                    console.log("delete file commond is deleted: "+filepath)
                }, 10000);
            }
        });
        // socket.on('m:play_finished', async(filepath) => {
        //     global.is_playing = false;    
        // });
        
    });

    return io;

}